/*
 *   Copyright 2011-2012 Hai Bison
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */
package group.pals.mailgate.beans;

import group.pals.mailgate.preferences.Preferences;
import group.pals.utils.text.StrUtils;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Hai Bison
 */
public class Utils {

    private final Preferences _Preferences;

    private Utils() {
        this._Preferences = Preferences.getInstance();
    }

    public static Utils getInstance() {
        return UtilsHolder.INSTANCE;
    }

    private static class UtilsHolder {

        private static final Utils INSTANCE = new Utils();
    }

    /*----------------------------------------------------------------------------
     * IMPLEMENTATION
     */
    /**
     * 
     * @param emailAccount
     * @return <code>ItemEmailProvider</code> or <code>null</code>.
     */
    public ItemEmailProvider getEmailProvider(ItemEmailAccount emailAccount) {
        List<ItemEmailProvider> emailProviders = _Preferences.getEmailProviders();
        if (emailAccount == null || emailAccount.getEmailAddress() == null) {
            return null;
        }

        for (ItemEmailProvider emailProvider : emailProviders) {
            if (emailAccount.getEmailAddress().matches(emailProvider.getEmailPattern())) {
                return emailProvider;
            }
        }
        return null;
    }

    public List<ItemGroupEmailMessages> groupEmailMessagesByConversation(List<ItemEmailMessage> emailMessages) {
        List<ItemGroupEmailMessages> result = new ArrayList<ItemGroupEmailMessages>();

        /* iterate all messages, group all messages that
         * have same sender and similar subjects in 90%
         */
        for (ItemEmailMessage emailMessage : emailMessages) {
            boolean found = false;
            for (ItemGroupEmailMessages group : result) {
                ItemEmailMessage newestMessage = group.getNewestMessage();
                if (newestMessage.getSender().equals(emailMessage.getSender())
                        && StrUtils.compareSimilarity(newestMessage.getSubject(), emailMessage.getSubject(), false) >= 90) {
                    group.add(emailMessage);
                    found = true;
                    break;
                }
            }//for group

            if (!found) {
                ItemGroupEmailMessages group = new ItemGroupEmailMessages();
                group.add(emailMessage);
                result.add(group);
            }
        }

        /*
         * just store the newest message, to save memory
         * TODO: this way is worst
         */
        for (ItemGroupEmailMessages group : result) {
            group.setCount(group.size());
            ItemEmailMessage newestMessage = group.getNewestMessage();
            group.clear();
            group.add(newestMessage);
        }

        return result;
    }
}
